<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8" />
  <title>图书管理</title>
  <link href="https://cdn.staticfile.org/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container mt-5">
  <h2>图书列表</h2>

  <!-- 仅管理员显示“添加图书”按钮 -->
  <a th:if="${#authorization.expression('hasRole(''ADMIN'')')}"
     href="/books/add"
     class="btn btn-primary mb-3">
    添加图书
  </a>

  <table class="table table-bordered">
    <thead>
    <tr>
      <th>ID</th>
      <th>书名</th>
      <th>作者</th>
      <th>ISBN</th>
      <th>库存</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="book : ${books}">
      <td th:text="${book.id}"></td>
      <td th:text="${book.title}"></td>
      <td th:text="${book.author}"></td>
      <td th:text="${book.isbn}"></td>
      <td th:text="${book.stock}"></td>
      <td>
        <a th:if="${#authorization.expression('hasRole(''ADMIN'')')}"
           th:href="@{'/books/edit/' + ${book.id}}"
           class="btn btn-sm btn-warning">编辑</a>

        <a th:if="${#authorization.expression('hasRole(''ADMIN'')')}"
           th:href="@{'/books/delete/' + ${book.id}}"
           class="btn btn-sm btn-danger"
           onclick="return confirm('确定删除该图书？')">删除</a>

        <a th:if="${#authorization.expression('hasRole(''USER'')')}"
           th:href="@{'/books/borrow/' + ${book.id}}"
           class="btn btn-sm btn-success">借阅</a>
      </td>
    </tr>
    </tbody>
  </table>

  <a href="/" class="btn btn-secondary">返回首页</a>
</div>
</body>
</html>
